package sskj.regulatory.presentation.screen.login


import android.content.Intent
import isskj.regulatory.presentation.base_mvp.api.ApiPresenter
import sskj.regulatory.BuildConfig
import sskj.regulatory.data.pref.Preferences
import sskj.regulatory.data.response.session.IndexDataResponse
import sskj.regulatory.data.response.session.TokenResponse
import sskj.regulatory.di.scope.PerActivity
import sskj.regulatory.domain.interactor.UserInteractor
import sskj.regulatory.ribble.presentation.utils.extensions.log
import java.util.*
import javax.inject.Inject

@PerActivity
class LoginPresenter @Inject constructor(private val preferences: Preferences) : ApiPresenter<LoginContract.View>(), LoginContract.Presenter {

    private val requestType = AUTH



    private val successAutoLogin = { _: TokenResponse ->
        view.openHomeActivity()
    }

    override fun autoLogin() {

        log { Date().time.toString() + "    " + preferences.time.toString() }
        log { Date().time - preferences.time }
        if (preferences.isExpired) {
            view.showLoading()
            fetch(userInteractor.refreshToken(), requestType, successAutoLogin)
        }
    }

    override fun makeLogin(mobile: String, password: String) {

        view.showLoading()

        when (BuildConfig.TEST) {
            true -> {
                fetch(userInteractor.login("13972608122", "123"), requestType){
                    when(it.success){
                        true->{
                            fetch(userInteractor.getIndexData(),requestType){
                                view.openHomeActivity()
                            }
                        }
                        else-> view.showError(it.msg)
                    }

                }

                // view.openHomeActivity()
            }
            false -> {
                if (mobile === BuildConfig.CLIENT_ID && password === BuildConfig.CLIENT_SECRET) {
                    view.openHomeActivity()
                } else {
                    when (mobile.isEmpty()) {
                        true -> {
                            view.showTipDialogError("用户名不能为空")
                            return
                        }
                    }

                    when (password.isEmpty()) {
                        true -> {
                            view.showTipDialogError("密码不能为空")
                            return
                        }
                    }
                    fetch(userInteractor.login(mobile, password), requestType){
                        when(it.success){
                            true->{
                                fetch(userInteractor.getIndexData(),requestType){
                                    view.openHomeActivity()
                                }
                            }
                            else-> view.showError(it.msg)
                        }

                    }
                }
            }
        }
    }

    override fun checkLogin(resultIntent: Intent?) {

    }

    override fun onRequestError(errorCode: String?, errorMessage: String?) {
        view.hideLoading()
        when (errorCode) {
            "401" -> {
                view.loginError("用户名或者密码错误,请重新输入。")
                view.clearText()
            }
            else -> view.loginError("登录失败,错误原因: ${errorMessage}。")

        }
    }

    override fun onConnectError() {
        view.hideLoading()
        super.onConnectError()
    }
}